package gui;


import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JToggleButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class RunSettingPanel extends JPanel{
	
	private static final long serialVersionUID = 1L;
	protected GameModel gameModel;
	private JSlider slider;
	private JToggleButton pauseButton; 
	

	public RunSettingPanel(GameModel gameModel){
		super(new GridBagLayout());
		this.gameModel = gameModel; 
		initGUI(); 
	}

	private void initGUI() {
		slider = new JSlider(JSlider.HORIZONTAL, 0, 1000, 500);
		slider.addChangeListener(new SliderListener());
		slider.setMajorTickSpacing(200);
		slider.setMinorTickSpacing(100);
		slider.setPaintTicks(true);
		slider.setPaintLabels(true);
		slider.setValue(200); 
		addComponent(slider, 0, 0); 
		
		pauseButton = new JToggleButton("Pause");
		pauseButton.addActionListener(new PauseSimulation()); 
		addComponent(pauseButton, 1, 0); 
	}
	
	protected void addComponent(Component comp, int i, int j) {
		GridBagConstraints cons = new GridBagConstraints(); 
		cons.gridx = i; 
		cons.gridy = j; 
		this.add(comp, cons); 
	}

	class SliderListener implements ChangeListener {

		@Override
		public void stateChanged(ChangeEvent e) {
			if (slider.getValue() == 0) {
				return; 
			}
			gameModel.setTimePerFrame(slider.getValue());
//			if (slider.getValue() == 0)
//				parameters.setViewMap(false);
//			else parameters.setViewMap(true);
		}
		
	}
	
	class PauseSimulation implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			JToggleButton btn = (JToggleButton) arg0.getSource();
			if (btn.isSelected()) {
				btn.setText("Continue");
				gameModel.pauseSimulation();
			}
			else {
				gameModel.continueSimulation();
				btn.setText("Pause");
			}
//			gameModel.pack(); 
		}
		
	}

	public void unPause() {
		if (pauseButton.isSelected()) {
			pauseButton.setSelected(false); 
			pauseButton.setText("Pause");
		}
	}
}
